package pe.com.hermes.hermetic.web.controller.general;

import pe.com.hermes.hermetic.beans.ClienteBean;
import pe.com.hermes.hermetic.negocio.service.HermeticFacade;
import pe.com.hermes.hermetic.negocio.ultil.Util;
import pe.com.hermes.hermetic.web.controller.base.IController;
import pe.com.hermes.hermetic.web.forms.SelecClienteForm;

import java.util.ArrayList;
import java.util.List;

import javax.servlet.http.HttpServletRequest;

import org.apache.log4j.Logger;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.SessionAttributes;

@Controller
@RequestMapping("/selecClienteInd.html") 
@SessionAttributes(value={"listClientes"})
public class SeleccionarClienteIndController implements IController {
    protected final static Logger logger = Logger.getLogger(SeleccionarClienteController.class);
    @Autowired
    private HermeticFacade servicioHermetic;
    
    @RequestMapping(method={RequestMethod.GET})  
    protected String showBuscarCliente(ModelMap model,HttpServletRequest request)throws Exception{
        try {
            logger.info(this.getClass().toString()+" "+"metodo: GET");
            SelecClienteForm form = new SelecClienteForm();
            form.setCliente((String)request.getSession(false).getAttribute("cliente"));
            form.setCodSucuHtb((String)request.getSession(false).getAttribute("sucursal"));
            
            List listClientes = new ArrayList();  
            
            listClientes = servicioHermetic.getListClieArchInd("");
                    
            String clieIndMarcado = (String)request.getSession(false).getAttribute("clieIndMarcado");             
            boolean encontro=false;
            for (int i=0;i<listClientes.size();i++){
                ClienteBean clie=(ClienteBean)listClientes.get(i);
                if (clie!=null && clie.getCodCliente()!=null && clie.getCodCliente().equals(clieIndMarcado)){
                    clie.setChkMarca("checked");
                    encontro=true;
                }
                else{
                    clie.setChkMarca("");
                }
            }
            if (!encontro){
                request.getSession(false).setAttribute("clieIndMarcado",null);
            }
            form.setListClientes(listClientes);
            request.getSession(false).setAttribute("listClientes",listClientes);
            
            model.put("listClientes",listClientes);
            model.put("formClie",form);
            return FORM_CARG_ARCH_IND_BUSC_CLIE;
        }
        catch (Exception e) {
            logger.error(e);
            return FORM_ERROR_POPUP_VIEW;
        }
    }
    
    @RequestMapping(params={"accion=buscarCliente","desCliente"}) 
    protected String buscarCliente(@RequestParam("desCliente") String desCliente ,ModelMap model,HttpServletRequest request)throws Exception{
                logger.info(this.getClass().toString()+" "+"metodo: GET");
        SelecClienteForm formClie = new SelecClienteForm();
        
        List listClientes = new ArrayList();        
                
        listClientes = servicioHermetic.getListClieArchInd(desCliente);
        
        String clieIndMarcado = (String)request.getSession(false).getAttribute("clieIndMarcado");             
        boolean encontro=false;
        for (int i=0;i<listClientes.size();i++){
            ClienteBean clie=(ClienteBean)listClientes.get(i);
            if (clie!=null && clie.getCodCliente()!=null && clie.getCodCliente().equals(clieIndMarcado)){
                clie.setChkMarca("checked");
            }
            else{
                clie.setChkMarca("");
            }
        }
        if (!encontro){
            request.getSession(false).setAttribute("clieIndMarcado",null);
        }
        if(desCliente.equals(""))
            request.getSession(false).setAttribute("listClientes",listClientes);
        model.put("listClientes",listClientes);
        model.put("formClie",formClie);
        return FORM_CARG_ARCH_IND_BUSC_CLIE;
    }
    
    @RequestMapping(params={"accion=selectCodCliente","codCliente"}) 
    protected @ResponseBody String[] selectCodCliente(@RequestParam("codCliente") String codCliente,HttpServletRequest request)throws Exception{        
        logger.info(this.getClass().toString()+" "+"metodo: selectCodCliente");        
        String[] chksCliente =new String[0];
        request.getSession(false).setAttribute("clieIndMarcado",codCliente);     
        return chksCliente;
    }
    
    @RequestMapping(params={"accion=deselectCodCliente","codCliente"}) 
    protected @ResponseBody String[] deselectCodCliente(@RequestParam("codCliente") String codCliente,HttpServletRequest request)throws Exception{        
        logger.info(this.getClass().toString()+" "+"metodo: deselectCodCliente");        
        String[] chksCliente =new String[0];
        //;
         return chksCliente;
    } 
    
    @RequestMapping(params={"accion=searchDesCliente"}) 
    protected @ResponseBody String[] searchDesCliente(HttpServletRequest request)throws Exception{        
        logger.info(this.getClass().toString()+" "+"metodo: searchDesCliente");        
        List listaCliente = (List)request.getSession(false).getAttribute("listClientes");
        String[] arrayCliente = new String[listaCliente.size()];
        for(int i=0;i<listaCliente.size();i++){
            arrayCliente[i] = ((ClienteBean)listaCliente.get(i)).getNombre();
        }
        return arrayCliente;
    }
    
}
